package com.gateway.plugin.security.oauth2.client;

import com.gateway.security.common.authentication.client.ClientPrincipal;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;
import java.util.Map;

/**
 * @program: api-gateway
 * @description:
 * @author: YuKai Fan
 * @create: 2025/2/11 21:57
 **/
public class OAuth2Client implements ClientPrincipal {

    /**
     * 用户名
     */
    private String clientId;

    /**
     * 租户名称
     */
    private String clientName;


    /**
     * 权限信息
     */
    private Collection<? extends GrantedAuthority> authorities;

    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public String getClientName() {
        return clientName;
    }

    public void setClientName(String clientName) {
        this.clientName = clientName;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public Object getPrincipal() {
        return this;
    }

    public void setAuthorities(Collection<? extends GrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    @Override
    public String getName() {
        return clientName;
    }


    public static final class Builder {
        private String clientId;
        private String clientName;
        private Collection<? extends GrantedAuthority> authorities;

        private Builder() {
        }

        public static Builder builder() {
            return new Builder();
        }

        public Builder clientId(String clientId) {
            this.clientId = clientId;
            return this;
        }

        public Builder clientName(String clientName) {
            this.clientName = clientName;
            return this;
        }

        public Builder authorities(Collection<? extends GrantedAuthority> authorities) {
            this.authorities = authorities;
            return this;
        }

        public OAuth2Client build() {
            OAuth2Client oAuth2Client = new OAuth2Client();
            oAuth2Client.setClientId(clientId);
            oAuth2Client.setClientName(clientName);
            oAuth2Client.setAuthorities(authorities);
            return oAuth2Client;
        }
    }
}
